﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

using PetaPoco;
using Marking.Dashboard.Web.DataContracts;
using Marking.Dashboard.Web.ServiceContracts;

namespace Marking.Dashboard.Web.Services
{
    public class FinanceService : IFinanceService
    {
        public IEnumerable<FinanceDTO> LoadFinanceInfos(string cycleType, DateTime startDate, DateTime endDate)
        {
            var db = this.CreateDb();
            db.EnableAutoSelect = false;

            var maps = new Dictionary<string, string>
            {
                { "年", "year"},
                { "月", "month"},
                { "日", "day"}
            };

            if (maps.ContainsKey(cycleType))
            {
                cycleType = maps[cycleType];
            }

            return db
                .Query<FinanceDTO>("EXEC [Pro_ReportFAAllData] @0, @1, @2", startDate, endDate, cycleType)
                .OrderBy(x => x.StartDate).ToList();
        }

        private Database CreateDb()
        {
            return new Database("Marking");
        }
    }
}